package eu.webhib.frmwrk.utils;

import eu.webhib.frmwrk.domain.intf.BaseEntity;
import eu.webhib.frmwrk.jsf.exception.FatalApplException;
import org.apache.log4j.Logger;

/**
 * Bean helper methods
 */
public class BeanUtils {

   private static final Logger log = Logger.getLogger(BeanUtils.class);

   private BeanUtils() {
   }

   /**
    * Helper to clone data entity and copy all attributes
    */
   public static <T extends BaseEntity> T cloneEntity(T src) {
      assert (src != null);
      try {
         /** cloned bean has same type as source */
         @SuppressWarnings("unchecked")
         T ret = (T) org.apache.commons.beanutils.BeanUtils.cloneBean(src);
         return ret;
      } catch (Exception ex) {
         log.error("cloneEntity: " + ex);
         throw new FatalApplException(ex);
      }
   }

   /**
    * Helper to copy properties of data entity
    */
   public static <T extends BaseEntity> void copyEntity(T dest, T src) {
      try {
         org.apache.commons.beanutils.BeanUtils.copyProperties(dest, src);
      } catch (Exception ex) {
         log.error("copyEntity: " + ex);
         throw new FatalApplException(ex);
      }
   }
}
